<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['ispay']=array('0'=>'未支付','1'=>'已支付');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'storeid'=>$this->store_id);
    $orderby='id desc';
	$total='';
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	$return['list']=$this->getslice('order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),			
		),
		'action'=>array(	
			array('type'=>'href','action'=>'store.s_item.s_detail','title'=>'订单详情'),
			array('op'=>'fa','title'=>'发货','type'=>'modal','show'=>array('status'=>1,'isdish'=>0)),
			array('op'=>'peisong','title'=>'指定配送','type'=>'modal','show'=>array('status'=>1,'isdish'=>1)),
			array('op'=>'print','title'=>'打印','type'=>'post'),
			array('type'=>'post','op'=>'shou','title'=>'确认收货','show'=>array('status'=>2)),
			
		),),
		'search'=>array(
			array('name'=>'ordersn','width'=>15,'title'=>'订单号','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('name'=>'buytype','width'=>15,'title'=>'订单类型','type'=>'radio','options'=>'buytype'),
			array('name'=>'sendtype','width'=>15,'title'=>'配送方式','type'=>'radio','options'=>'sendtype'),
			array('name'=>'merchid','width'=>15,'title'=>'所属商户','type'=>'select','option_value'=>'id','option_title'=>'merchname','options'=>'merchs'),
		)
	);
	$return['htmls']=$htmls;
	if($this->plugin_exists('jy_merch')){
		$merchs=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'));
	}else{
		$merchs=array();
	}
	array_unshift($merchs,array('id'=>0,'merchname'=>'平台自营'));
	$return['merchs']=$merchs;
	exi($return);
}
if($_RQ['op']=='export'){
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$params['storeid']=$this->store_id;
	$order=pdo_getall('jy_weishop_order',$params);
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('1'=>'微信支付','2'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');		
		$address=pdo_get('core_address',array('id'=>$v['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$v['address']=model('core/member')->get_address_str($v['addressid']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
	}
	$header=array(
		'ordersn'=>'订单号',
		'nickname'=>'买家昵称',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'金额',
		'paytype'=>'订单状态',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}
if($_RQ['op']=='fa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['sendtype']==1){
			if(!$post['express'] || !$post['expresssn']){
				exi('请填写快递信息','error');
			}
			$data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
			$this->model('order')->order_send($id,$data);
		}else{
			$this->model('order')->order_send_virtual($id,$post['content']);
		}
		
		exi('已发货！','','//list/status=2');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','sendtype'));
	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单发货',
	);
	if($item['sendtype']==1){
		$htmls['edit']=array(
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name'),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn'),
		);
		$return['express']=$this->express_types;
	}else{
		$goodsid=$this->pdo_getcolumn('order_goods',array('orderid'=>$id),'goodsid');
		$goods=$this->pdo_get('goods',array('id'=>$goodsid),array('title','image'));
		$item=array_merge($goods,$item);
		$htmls['edit']=array(
			array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
			array('type'=>'image','title'=>'商品图片','name'=>'image','read'=>true),
			array('type'=>'text','title'=>'发货内容','name'=>'content','help'=>'虚拟物品发货，填写发货内容！'),
			
		);
	}
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='peisong'){
	$install = check_account_permission('jy_peisong');
	if(!$install){
		exi('请联系管理员开通《配送员》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if(!$post['paisong_id']){
			exi('请选择配送员！','error');
		}
		$params['order_id']=$id;
		$params['peisong_id']=$post['paisong_id'];
		model('jy_peisong/order')->order_get($params);		
		exi('指定配送员成功！','','//list/status=2');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','sendtype','storeid'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单指定配送',
	);
	$htmls['edit']=array(
			array('type'=>'select','title'=>'指定配送员','name'=>'paisong_id','options'=>'peisongs','help'=>'只能指定该门店下面的配送员'),
	);
	$peisongs=array();
	$areas=pdo_getall('jy_peisong_area',array('store_id'=>$item['storeid']));
	foreach($areas as $item){
				$users=pdo_getall('jy_peisong_member',array('group_id'=>$item['id'],'status'=>1),array('id','title'));
				foreach($users as $uv){
					$peisongs[]=$uv;
				}
	}
	$return['peisongs']=$peisongs;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='shou'){
	$id=intval($_RQ['id']);
	$this->model('order')->order_receive($id);
	exi('已收货','','//list/status=3');
}
if($_RQ['op']=='print'){
	$order_id=intval($_RQ['id']);
	$res=$this->model('printmsg')->storeprint('',$order_id,0,1);
	if($res){
		exi('打印成功');
	}else{
		exi('打印失败,该订单所属门店没有添加打印任务！','error');
	}
}
if($_RQ['op']=='deleted'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>1);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['member']=cfc('core')->member_show($v['mid']);
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'ddel','list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),				
		),
		'action'=>array(	
			array('type'=>'href','action'=>'order.detail','title'=>'订单详情'),
		),),
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$this->model('order')->order_delete($id);
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->model('order')->order_delete($id);
			}
		}
	}
	exi('删除成功！','','list');
}
if($_RQ['op']=='ddel'){

	$id=intval($_RQ['id']);
	if($id){
		$this->delete('order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！','','deleted');
}
?>